

Ext.define('BUGS.view.Login' ,{
    extend: 'Ext.panel.Panel',
    alias : 'widget.login',

    layout: {
   	 type: 'vbox',
   	 align: 'center'
    },
	items: [{
       xtype: 'form',
       margin: '20,0,0,0',
       width: 450,
       height: 250,
       title: 'Login',
       layout: {
       	type: 'vbox',
       	align: 'center'
       },
       items: [{
           	xtype: "textfield",
           	fieldLabel: 'Username',
   			name: 'utenti.username',
   			margin: '15,0,0,0',
   			allowBlank: false
			},
			{
				fieldLabel: 'Password',
				name: 'utenti.password',
				xtype:'textfield',
				inputType: 'password',
				margin: '5,5,5,5',
				allowBlank: false      				
			}
       ],
       buttons: [{
			xtype: 'button',
			text: 'Ok',
			formBind: true,
			disabled: false,
			handler: function(){
				var form = this.up('form').getForm();
				if(form.isValid()){
					doLogin(this,form);
	    		}
			}
		},{
			xtype: 'button',
			text: 'Annulla',
			handler: function(){
	    		  this.up('form').getForm().reset();
			}
		}
		       
		]
   
		
	}
	]
});



function doLogin(theButton,theForm){
	var _params = theForm.getValues();
	Ext.Ajax.request({
	    url: 'login.do?'+document.cookie,
	    method: 'post',
	    params: _params,
	    success: function(response){
	    	alert(response.responseText);
	        var text = eval(response.responseText);
	        if(!text[0].result){
	        	var msg = '';
	        	for(var i=0 ; i < text[0].messages.length;i++){
	        		msg+=text[0].messages[i]+'\n\r';
	        	}
	        	Ext.Msg.alert('Failure', msg);
	    	}else{
	    		eval(text[0].method);
	    	}
		}
	});
	
}

function drawAdminWindow(){
	alert('ciao');
	Ext.create('Ext.container.Viewport', {
        layout: 'fit',
        
        items: [{
        	xtype: 'panel'
        }
        ],
        renderTo: document.body
    });
}